package com.tobeface.wechat.message.handler;

import com.google.common.base.Objects;
import com.tobeface.wechat.message.WeChatMessage;
import com.tobeface.wechat.message.WeChatMessageChannel;
import com.tobeface.wechat.message.WeChatMessagePayloads;
import com.tobeface.wechat.message.WeChatMessageTypes;

/**
 * @author loudyn
 */
public abstract class ImageMessageHandlerSupport extends WeChatMessageHandlerSupport {

    @Override
    protected boolean isMessageTypeSupport(String msgType, WeChatMessage message) {
        if (!Objects.equal(msgType, WeChatMessageTypes.IMAGE)) {
            return false;
        }

        String picUrl = (String) message.getPayloads().get(WeChatMessagePayloads.PICURL_PAYLOAD);
        String mediaId = (String) message.getPayloads().get(WeChatMessagePayloads.MEDIAID_PAYLOAD);
        return isImageMessageSupport(message, picUrl, mediaId);
    }

    protected boolean isImageMessageSupport(WeChatMessage message, String picUrl, String mediaId) {
        return true;
    }

    @Override
    protected final void onMessageReceived(WeChatMessageChannel channel, WeChatMessage message) {
        String picUrl = (String) message.getPayloads().get(WeChatMessagePayloads.PICURL_PAYLOAD);
        String mediaId = (String) message.getPayloads().get(WeChatMessagePayloads.MEDIAID_PAYLOAD);
        onImageMessageReceived(channel, message, picUrl, mediaId);
    }

    protected abstract void onImageMessageReceived(WeChatMessageChannel channel, WeChatMessage message,
                                                   String picUrl, String mediaId);
}
